package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.WelcomeAdapter;
import com.kaisa.activity.bean.DaysSell;
import com.kaisa.activity.bean.HomeImg;
import com.kaisa.activity.bean.StartContaints;
import com.kaisa.activity.bean.StartRoad;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 123 on 2015/12/19.
 */
public class HotHomeProduceActivity extends AppCompatActivity {
    @Bind(R.id.vp_product)
    ViewPager vpProduct;
    @Bind(R.id.ll_dot_product)
    LinearLayout llDotProduct;
    @Bind(R.id.tv_product_lineName)
    TextView tvProductLineName;
    @Bind(R.id.tv_product_lineTypeName)
    TextView tvProductLineTypeName;
    @Bind(R.id.tv_product_presalePrice)
    TextView tvProductPresalePrice;
    @Bind(R.id.tv_product_left)
    TextView tvProductLeft;
    @Bind(R.id.tv_product_remainSignUpNum)
    TextView tvProductRemainSignUpNum;
    @Bind(R.id.tv_product_bottom)
    TextView tvProductBottom;
    @Bind(R.id.tv_product_retailPrice)
    TextView tvProductRetailPrice;
    @Bind(R.id.tv_product_next)
    TextView tvProductNext;
    @Bind(R.id.tv_product_startDate)
    TextView tvProductStartDate;
    @Bind(R.id.tv_product_departureName)
    TextView tvProductDepartureName;
    @Bind(R.id.tv_product_toairlineCompany)
    TextView tvProductToairlineCompany;
    @Bind(R.id.tv_product_tolineInfoFlight)
    TextView tvProductTolineInfoFlight;
    @Bind(R.id.tv_product_backairlineCompany)
    TextView tvProductBackairlineCompany;
    @Bind(R.id.tv_product_backlineInfoFlight)
    TextView tvProductBacklineInfoFlight;
    @Bind(R.id.tv_product_tourGroupPeopleNum)
    TextView tvProductTourGroupPeopleNum;
    @Bind(R.id.tv_product_join)
    TextView tvProductJoin;
    @Bind(R.id.tv_product_score)
    TextView tvProductScore;
    @Bind(R.id.tv_product_joinpeoplenum)
    TextView tvProductJoinpeoplenum;
    @Bind(R.id.tv_product_visa)
    TextView tvProductVisa;
    @Bind(R.id.tv_product_feature)
    TextView tvProductFeature;
    @Bind(R.id.tv_product_lineFeature)
    TextView tvProductLineFeature;
    @Bind(R.id.tv_product_attributes)
    TextView tvProductAttributes;
    @Bind(R.id.tv_product_phone)
    TextView tvProductPhone;
    @Bind(R.id.tv_product_line)
    TextView tvProductLine;
    @Bind(R.id.tv_product_consult)
    TextView tvProductConsult;
    @Bind(R.id.tv_product_book)
    TextView tvProductBook;
    @Bind(R.id.ll_product)
    LinearLayout llProduct;
    private WelcomeAdapter adapter;
    private List<ImageView> imgs = new ArrayList<ImageView>();
    private  DaysSell.ThemeEntity bean;
    private List<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        bean = (DaysSell.ThemeEntity) extras.getSerializable("bean");
        initView();
        initData();
    }

    private void initData() {
        x.http().get(new RequestParams(UrlHttp.PRODUCE_IMG + bean.getProductId()), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                HomeImg img = (HomeImg) JSON.parseObject(s, HomeImg.class);
                for (int i = 0; i <img.getMatches().size() ; i++) {
                    list.add(img.getMatches().get(i).getEntityUrl());
                }
                Message msg = handler.obtainMessage();
                msg.what=1;
                msg.obj = list;
                handler.sendMessage(msg);
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
        x.http().get(new RequestParams(UrlHttp.PRODUCE_START + bean.getGroupId()), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                StartContaints entity = JSON.parseObject(s,StartContaints.class);
                tvProductPresalePrice.setText("￥"+entity.getMatches().get(0).getPresalePrice());
                tvProductRemainSignUpNum.setText(entity.getMatches().get(0).getRemainSignUpNum());
                tvProductRetailPrice.setText(entity.getMatches().get(0).getRetailPrice());
                tvProductStartDate.setText("最近出团   "+entity.getMatches().get(0).getStartDate());
                tvProductDepartureName.setText("出发地   "+entity.getMatches().get(0).getDepartureName());
                tvProductTolineInfoFlight.setText(entity.getMatches().get(0).getAttributes().getSource_1().get(0).getLineInfoFlight());
                tvProductBacklineInfoFlight.setText(entity.getMatches().get(0).getAttributes().getSource_1().get(0).getLineInfoFlight());
                tvProductToairlineCompany.setText(entity.getMatches().get(0).getAttributes().getSource_1().get(0).getAirlineCompany());
                tvProductBackairlineCompany.setText(entity.getMatches().get(0).getAttributes().getSource_1().get(0).getAirlineCompany());
                tvProductTourGroupPeopleNum.setText("   团队规模  "+entity.getMatches().get(0).getTourGroupPeopleNum()+"  已报");
                tvProductJoin.setText(Integer.parseInt(entity.getMatches().get(0).getTourGroupPeopleNum())-Integer.parseInt(entity.getMatches().get(0).getRemainSignUpNum())+" ");
                tvProductScore.setText(entity.getMatches().get(0).getScore());
                tvProductJoinpeoplenum.setText(entity.getMatches().get(0).getJoinpeoplenum());
                tvProductVisa.setText(entity.getMatches().get(0).getVisa());
                tvProductFeature.setText(entity.getMatches().get(0).getProFeature());
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        adapter = new WelcomeAdapter(imgs);
        vpProduct.setAdapter(adapter);
        vpProduct.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i <list.size() ; i++) {
                    llDotProduct.getChildAt(i).setEnabled(true);
                }
                llDotProduct.getChildAt(position).setEnabled(false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    if(msg.obj!=null){
                        List<String> ls = (List<String>) msg.obj;
                        for (int i = 0; i <ls.size() ; i++) {
                            ImageView iv = new ImageView(HotHomeProduceActivity.this);
                            x.image().bind(iv,ls.get(i));
                            imgs.add(iv);
                        }
                        for (int i = 0; i <ls.size(); i++) {
                            ImageView dots = new ImageView(HotHomeProduceActivity.this);
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                            dots.setLayoutParams(params);
                            dots.setScaleType(ImageView.ScaleType.FIT_XY);
                            dots.setBackground(getResources().getDrawable(R.drawable.bg_dots));
                            llDotProduct.addView(dots);
                        }
                        llDotProduct.getChildAt(0).setEnabled(false);
                        adapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };
}
